﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Windows.Forms;

namespace TVPlayer
{
    public enum InformationPanelDisplayModes { AlwaysHide, AlwaysShow, ShowOnchannelChange }

    public class Configuration
    {
        private bool initialized;

        private bool connectOnStartup;
        private string mediaPortalServer;
        private string vlcLocation;
        private string vlcDeinterlace;
        private InformationPanelDisplayModes informationPanelDisplayMode = InformationPanelDisplayModes.ShowOnchannelChange;
        private bool closeEPGonChannelChange;
        private List<string> vlcOptions = new List<string>();
        private bool showEPGPanel;
        private bool showInfoPanel;
        private bool tuneLastChannelOnStartup;
        private string lastChannel;

        public bool Initialized
        {
            get { return initialized; }
        }

        public bool ConnectOnStartup
        {
            get { return connectOnStartup; }
            set { connectOnStartup = value; }
        }

        public string MediaPortalServer
        {
            get { return mediaPortalServer; }
            set { mediaPortalServer = value; }
        }

        public string VlcLocation
        {
            get { return vlcLocation; }
            set { vlcLocation = value; }
        }

        public string VlcDeinterlace
        {
            get { return String.IsNullOrEmpty(vlcDeinterlace) ? "Discard" : vlcDeinterlace; }
            set { vlcDeinterlace = value; }
        }

        public InformationPanelDisplayModes InformationPanelDisplayMode
        {
            get { return informationPanelDisplayMode; }
            set { informationPanelDisplayMode = value; }
        }

        public bool CloseEPGOnChannelChange
        {
            get { return closeEPGonChannelChange; }
            set { closeEPGonChannelChange = value; }
        }

        public List<string> VLCOptions
        {
            get { return vlcOptions; }
            set { vlcOptions = value; }
        }

        public bool ShowEPGPanel
        {
            get { return showEPGPanel; }
            set { showEPGPanel = value; }
        }

        public bool ShowInfoPanel
        {
            get { return showInfoPanel; }
            set { showInfoPanel = value; }
        }

        public bool TuneLastChannelOnStartUp
        {
            get { return tuneLastChannelOnStartup; }
            set { tuneLastChannelOnStartup = value; }
        }

        public string LastChannel
        {
            get { return lastChannel; }
            set { lastChannel = value; }
        }

        public static Configuration Load()
        {
            if (!File.Exists(GetFilename()))
            {
                return new Configuration();
            }

            XmlSerializer xmls = new XmlSerializer(typeof(Configuration));
            TextReader tr = new StreamReader(GetFilename());
            Configuration cfg = (Configuration)xmls.Deserialize(tr);
            tr.Close();

            cfg.initialized = true;

            return cfg;
        }

        public static void Save(Configuration cfg)
        {
            XmlSerializer xmls = new XmlSerializer(typeof(Configuration));
            TextWriter tw = new StreamWriter(GetFilename());
            xmls.Serialize(tw, cfg);
            tw.Close();
        }

        public static string GetFilename()
        {
            return Path.GetDirectoryName(Application.ExecutablePath) + "\\Configuration.xml";
        }
    }
}
